package com.sysu.ooad.action;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.Action;
import com.sysu.ooad.entity.Teacher;
import com.sysu.ooad.service.CourseService;
import com.sysu.ooad.service.StudentService;
import com.sysu.ooad.service.TeacherService;
import com.sysu.ooad.util.AjaxCommand;
import com.sysu.ooad.util.JsonConfigFactory;
import com.sysu.ooad.util.TeacherJsonConfigFactory;

@Component
@Scope("prototype")
public class TeacherAction extends CommonAction {
	
	private static final long serialVersionUID = 1L;
	private String name;
	private String password;
	private TeacherService teacherService;
	private StudentService studentService;
	private CourseService courseService;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public TeacherService getTeacherService() {
		return teacherService;
	}

	public StudentService getStudentService() {
		return studentService;
	}
	@Resource
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}
	@Resource
	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}
	public CourseService getCourseService() {
		return courseService;
	}
	@Resource
	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}
	
	public String login() throws Exception {
		Teacher t = teacherService.login(name, password);
		if(t != null) {
			session.put("teacher", t);
			return Action.SUCCESS;
		}
		return Action.ERROR;
	}
	
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	public String addAssistant() throws Exception {
		Teacher t = this.findCurrentTeacher();
		Teacher assistant = teacherService.addAssistant(name, password, t.getCourse());
		JsonConfigFactory jsonConfigFactory = new TeacherJsonConfigFactory();
		HttpServletResponse response = ServletActionContext.getResponse();
		AjaxCommand.ajaxReturnJSON(response, assistant, jsonConfigFactory.createJsonConfig());
		return null;
	}
}
